package com.label.Web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.label.Config.Logger.LogAnnotation;
import com.label.Model.Common.ResponseBean;
import com.label.Model.Common.Validation.Save;
import com.label.Model.Do.User;
import com.label.Model.Query.RoleQuery;
import com.label.Service.RoleService;
import com.label.Service.UserService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/role")
public class RoleController {

    private final static Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Resource
    private RoleService roleService;

    @Resource
    private UserService userService;

    @Resource
    @Qualifier("oracleJdbcTemplate")
    private JdbcTemplate oracleJdbc;


    @RequestMapping("/add")
    @LogAnnotation(module = "角色模块",operator = "添加角色")
    @RequiresAuthentication
    public ResponseBean add(@Validated(value = Save.class) @RequestBody RoleQuery roleQuery){
        try{
            Boolean flag=roleService.addRole(roleQuery);
            if(flag)
                return new ResponseBean(200,"成功", true);
            return new ResponseBean(400,"角色名重复", false);
        } catch (Exception e){
            e.printStackTrace();
            logger.error("传入参数：" + roleQuery + "/n" + e.getMessage());
            return new ResponseBean(500,"失败",false);
        }
    }

    @RequestMapping("/update")
    @LogAnnotation(module = "角色模块",operator = "更新角色")
    @RequiresAuthentication
    public ResponseBean update(@RequestBody RoleQuery roleQuery){

        try{
            Boolean flag=roleService.updateRole(roleQuery);
            if(flag)
                return new ResponseBean(200,"成功", true);
            return new ResponseBean(400,"角色不存在", false);
        } catch (Exception e){
            e.printStackTrace();
            logger.error("传入参数：" + roleQuery + "/n" + e.getMessage());
            return new ResponseBean(500,"失败",false);
        }

    }

    @RequestMapping("/del")
    @LogAnnotation(module = "角色模块",operator = "删除角色")
    @RequiresAuthentication
    public ResponseBean del(@RequestBody RoleQuery roleQuery){
        try{
            Boolean flag=roleService.delRole(roleQuery);
            if(flag)
                return new ResponseBean(200,"成功", true);
            return new ResponseBean(400,"角色不存在", false);
        } catch (Exception e){
            e.printStackTrace();
            logger.error("传入参数：" + roleQuery + "/n" + e.getMessage());
            return new ResponseBean(500,"失败",false);
        }
    }

    @RequestMapping("/getAll")
    @LogAnnotation(module = "角色模块",operator = "获取所有角色")
    @RequiresAuthentication
    public ResponseBean getAll(){
        return new ResponseBean(200,"成功", JSONObject.toJSONString(roleService.getAllRole()));
    }

    @RequestMapping("/getOne")
    @LogAnnotation(module = "角色模块",operator = "获取一个角色")
    @RequiresAuthentication
    public ResponseBean getOne(@RequestBody RoleQuery roleQuery){
        if(roleQuery.getId() !=null)
            return new ResponseBean(200,"成功",JSONObject.toJSONString(roleService.getRoleById(roleQuery)));
        return new ResponseBean(400,"参数错误",null);
    }

    @RequestMapping("/getUserByRoles")
    @LogAnnotation(module = "角色模块",operator = "根据角色获取用户")
    @RequiresAuthentication
    public ResponseBean getUserByRoles(@RequestParam Long roleid){
        List<Map<String,Object>> users=oracleJdbc.queryForList("select cast(user_id as varchar(20)) as userid from user_role where role_id='"+roleid+"'");
        if(users.size()>0){
            List<Long> ids=new ArrayList<>();
            for(Map<String,Object> m:users)
                ids.add(Long.parseLong(m.get("userid").toString()));
            List<User> userDOS=userService.listByIds(ids);
            return new ResponseBean(200,"查询成功", JSON.toJSONString(userDOS, SerializerFeature.DisableCircularReferenceDetect));
        }
        return new ResponseBean(200,"查询成功",new ArrayList<>());
    }


}
